<template>
    <Container class="single-sale">
      <div class="container-header title title-center" slot="header">单品促销
        <Button size="large" class="fl btn-back"  icon="ios-arrow-back" @click="jumpPage">返回</Button>
      </div>
      <!-- 基础设置 -->
      <DefaultEditItem ref="defaultEdit" :form="$store.state.singleSale.form"></DefaultEditItem>
      <div class="container-footer bg-white" slot="footer">
        <Button type="primary" size="large" class="ml" @click="onClickNext">下一步</Button>         
      </div>
    </Container>
</template>
<script>
import DefaultEditItem from './edit/DefaultEditItem';
export default {
  components: {
    DefaultEditItem
  },
  data () {
    return {
      isEmpty: false, // 是否清空数据
      isSave: true, // 是否保存数据
      id: this.$route.query.id || '',
      type: this.$route.query.type || ''
    }
  },
  mounted() {
  },
  methods: {
    getDetailData() {
      this.$ajaxGet('/activity/single/detail/' + `${this.id}`).then(data => {
          this.$store.state.singleSale.form.remarks = data.activityInfoDTO.activityDescription//活动备注
          this.$store.state.singleSale.form.name = data.activityInfoDTO.activityName //促销名称
          this.$store.state.singleSale.form.grade = this.promotionMemberScope = data.activityInfoDTO.storeTypeEnumList, //活动对象
          this.$store.state.singleSale.form.date = [new Date(data.activityInfoDTO.startDate), new Date(data.activityInfoDTO.endDate)]
          this.$store.state.singleSale.form.slogan = data.activitySingleDTO.advertisingSlogan //宣传语
          this.$store.state.singleSale.form.type = data.activitySingleDTO.singlePromotionTypeEnum == 'TIME_LIMIT' ? '2' : '1'
          this.$store.state.singleSale.form.term = data.activitySingleDTO.isLimitOneOrder ? [2] : []
          this.$store.state.singleSale.form.num = data.activitySingleDTO.isLimitQuantity ? '2' : '1'
          this.$store.state.singleSale.form.least = data.activitySingleDTO.limitMin
          this.$store.state.singleSale.form.max = data.activitySingleDTO.limitMax
      })
    },
    onClickNext() {
      let self = this;
      this.$refs.defaultEdit.$refs.form.validate(function(param){
        if(param) {
          let url = {name: 'sales/single/select', query: {}}
          if (self.id) {
            url.query.id = self.id
            if (self.type) url.query.type = self.type
          }          
          self.$router.push(url)
        }
      })
    },
    jumpPage() {
      this.$router.back()
    }
  },
  beforeRouteEnter(to, from, next) {
      /*
        1. 路由从不是下一页进入的都清空
        2. 路由从下一页进入并且有id就不刷新接口--保存信息
      */
      next(vm=> {
        if (from.name !== 'sales/single/select') {
          vm.isEmpty = true
        } else {
          vm.isSave = false
        }
        if (vm.isEmpty) vm.$store.commit('setSingleSaleEmpty')
        if (vm.id && vm.isSave) vm.getDetailData()
      })
  }
}
</script>

